package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.CommonQuestionListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.CommonQuestion;
import com.iexbuy.ihg.entity.CommonQuestionListObjectResponce;
import com.iexbuy.ihg.entity.Shop;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class WeDeCommonQuestionsFragment extends BaseFragment {

	private static WeDeCommonQuestionsFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;


	private List<CommonQuestion> commonQuestions = new ArrayList<CommonQuestion>();
	private int pageNo = 0;
	private CommonQuestionListAdapter commonQuestionListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_common_question, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_common_questions));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		initListData();
	}


	public void initListData() {

		HashMap<String, String> paramMap = new HashMap<String, String>();
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_COMMON_QUESTION_LIST, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						CommonQuestionListObjectResponce rep = JsonUtils.parseJsonStrToObject(responseInfo.result, CommonQuestionListObjectResponce.class);
						commonQuestions.clear();
						commonQuestions.addAll(rep.getBody());
						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
					}
				});
	}

	private void initListView() {
		Collections.sort(commonQuestions, new Comparator<CommonQuestion>() {
			@Override
			public int compare(CommonQuestion commonQuestion, CommonQuestion t1) {
				int number1 = commonQuestion.getTitle().charAt(0);
				int number2 = t1.getTitle().charAt(0);
				return new BigDecimal(number1).compareTo(new BigDecimal(number2));
			}
		});
		if (commonQuestionListAdapter == null) {
			commonQuestionListAdapter = new CommonQuestionListAdapter(mBaseActivity, commonQuestions);
			listView.setAdapter(commonQuestionListAdapter);
		}
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				WeDeCommonQuestionDetailFragment.setCommonQuestion(commonQuestions.get(i));
				CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCommonQuestionDetailFragment.class);
			}
		});
		commonQuestionListAdapter.notifyDataSetChanged();
	}


	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeCommonQuestionsFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
